package org.yu.register.capability;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.HashMultiset;
import net.minecraft.core.Registry;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.Items;
import net.minecraftforge.common.util.INBTSerializable;
import org.yu.ItemData;

import java.util.function.BiConsumer;
import java.util.function.Consumer;

public class MultiDustCapability implements INBTSerializable<CompoundTag> {
    public final HashMultiset<Item> set=HashMultiset.create();
    public int count(Item item){
        return set.count(item);
    }
    @Override
    public CompoundTag serializeNBT() {
        CompoundTag tag=new CompoundTag();
        set.forEach(key->tag.putInt(key.getRegistryName().toString(),set.count(key)));
        return null;
    }
    public void safeForEach(Consumer<Item> safeArgs){
        set.stream().filter(ItemData::has).forEach(safeArgs);
    }
    @Override
    public void deserializeNBT(CompoundTag nbt) {
        nbt.getAllKeys().forEach(key->{
           var item=Registry.ITEM.get(new ResourceLocation(key));
           if(!item.equals(Items.AIR))
               set.add(item,nbt.getInt(key));
        });
    }
}
